<template>
  <div>
     <ul>
      <li v-for="m in messages" :key="m.id">
        <!-- <router-link :to="'/home/message/detail/'+m.id">{{m.title}}</router-link> -->
        <!-- <router-link :to="`/home/message/detail/${m.id}?id2=${m.id}&title=${m.title}`">{{m.title}}</router-link> -->
        <router-link :to="{
          name: 'MessageDetail', 
          params: {id: m.id}, 
          query:{id2: m.id, title: m.title}}">{{m.title}}</router-link>
        <button @click="pushShow(m)">push查看</button>
        <button @click="replaceShow(m)">replace查看</button>
      </li>
    </ul>
    <button @click="$router.back()">返回</button>
    <hr>

    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'Message',
  data () {
    return {
      messages: []
    }
  },

  methods: {
    pushShow (m) {
      // 进行编程式路由跳转
      // push(location)
      // 字符串形式
      // this.$router.push(`/home/message/detail/${m.id}?id2=${m.id}&title=${m.title}`)
      // 对象形式
      this.$router.push({
        name: 'MessageDetail',
        // path: '/home/message/detail', // 一旦需要指定params, 只能用name
        params: {id: m.id},
        query: {
          id2: m.id,
          title: m.title
        }
      })
    },

    replaceShow (m) {
      // 进行编程式路由跳转
      this.$router.replace(`/home/message/detail/${m.id}?id2=${m.id}&title=${m.title}`)
    }
  },

  mounted () {
    // 模拟请求获取messages
    setTimeout(() => {
      const messages = [
        {id: 1, title: 'Message00001'},
        {id: 3, title: 'Message00003'},
        {id: 5, title: 'Message00005'},
      ]
      this.messages = messages
    }, 1000)
  }
}
</script>

<style scoped>

</style>
